<template>
  <div class="shanchudizhi">
    <div class="del-zhi-top">
      <van-icon @click="$router.go(-1)" name="arrow-left" />
      <b>删除/修改收货地址</b>
      <span class="del-zhi" @click="del">删除</span>
    </div>
    <div class="del-center">
      <div class="del-center1">
        <div>姓名：</div>
        <input type="text" v-model="inp1" />
      </div>
      <div class="del-center1">
        <div>手机号码：</div>
        <input type="text" v-model="inp2" />
      </div>
      <div class="del-center1">
        <div>所在地区：</div>
        <input type="text" v-model="inp3" />
      </div>
      <div class="del-center1">
        <div>详细信息：</div>
        <input type="text" v-model="inp4" />
      </div>
      <div class="del-center1">
        <van-checkbox v-model="checked">设为默认地址</van-checkbox>
      </div>
      <button @click="save">保存并使用</button>
    </div>
  </div>
</template>
<script>
import axios from "axios";
export default {
  data() {
    return {
      inp1: this.$route.query.code.receiver,
      inp2: this.$route.query.code.mobile,
      inp3: this.$route.query.code.regions,
      inp4: this.$route.query.code.address,
      checked: true
    };
  },
  created() {
    console.log(this.$route.query.code);
  },
  methods: {
    // 保存按钮点击
    save() {
      console.log(this.inp1, this.inp2, this.inp3, this.inp4);
      console.log(this.checked);
      console.log(this.$route.query.code._id);
      let uid = localStorage.getItem("userid");
      let token = localStorage.getItem("token");
      // console.log(uid);
      // console.log(token);

      axios
        .put(
          `http://182.92.222.190:3000/api/v1/address/amend/${this.$route.query.code._id}`,
          {
            user: uid,
            isDefault: this.checked,
            regions: this.inp3,
            address: this.inp4,
            receiver: this.inp1,
            mobile: this.inp2,
            headers: {
              yangnb: `ynb ${token}`
            }
          }
        )
        .then(res => {
          console.log(res);
          this.$router.push({ name: "Delivery" });
        })
        .catch(err => {
          console.log(err);
        });
    },
    // 删除按钮点击
    del() {
      axios
        .delete(
          `http://182.92.222.190:3000/api/v1/address/delete/${this.$route.query.code._id}`,
          {
            id: this.$route.query.code._id
          }
        )
        .then(res => {
          console.log(res);
          this.$toast(res.data.mag);
          this.$router.push({ name: "Delivery" });
        });
    }
  }
};
</script>
<style scoped>
@import "../css/shanchudizhi.css";
</style>